package com.yndsoft.jc.ui.memberpage.myorder;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseFragment;
import com.yndsoft.jc.model.Order;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.netutils.AppUtils;
import com.yndsoft.jc.netutils.DensityUtil;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;
import com.yndsoft.jc.utils.ConstantValues;
import com.yndsoft.jc.widget.SlideRecycleView;
import com.yndsoft.jc.widget.SlideRecycleViewAdapter;
import com.yndsoft.jc.widget.SlideRecycleViewItemDecoration;
import com.yndsoft.jc.widget.SlideRecycleViewItemVerticalDecoration;
import com.yndsoft.jc.widget.SlideViewHolder;
import com.yndsoft.jc.widget.pulltorefresh.PullToRefreshBase;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by hasee on 2017/12/6.
 */

public class WholeFragment extends BaseFragment implements SlideRecycleViewAdapter.OnItemClickListener {

    private static final String ARG_POSITION = "position";
    @Bind(R.id.order_list)
    SlideRecycleView mRecycleView;
    @Bind(R.id.score_quesheng)
    LinearLayout score_quesheng;
    //    List<String> mData = new ArrayList<>();
    private int postion;
    private ScoreListAdapter mScoreAdapter;
    private ArrayList<Order> mDatas = new ArrayList<Order>();
    private TextView loadAllView;
    private int start = 0;

    @Override
    public int getContentResouceId() {
        return R.layout.fragment_order_whole;
    }

    public static WholeFragment newInstance(int position) {
        WholeFragment orderListFragment = new WholeFragment();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        orderListFragment.setArguments(b);
        return orderListFragment;
    }

    @Override
    public String getFragmentName() {
        return null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        postion = getArguments().getInt(ARG_POSITION);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setViews();
        getDatas(RefreshType.PULL_DWON_TO_REFRESH, start);
    }

    private void setViews() {
//        SlideRecycleViewItemVerticalDecoration item = new SlideRecycleViewItemVerticalDecoration(getActivity(), SlideRecycleViewItemDecoration.VERTICAL_LIST, getResources().getDrawable(R.drawable.driver_line1));
//        item.setIsShowSecondItemDecoration(false);
//        mRecycleView.getRefreshableView().addItemDecoration(item);  //添加分割线
        mRecycleView.setScrollingWhileRefreshingEnabled(false); //设置刷新的时候不可滑动

        mRecycleView.getRefreshableView().setLayoutManager(new LinearLayoutManager(getActivity())); //添加布局管理器
        mRecycleView.setMode(PullToRefreshBase.Mode.BOTH);
        mRecycleView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<RecyclerView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
//                mScoreAdapter.reMoveFooterView(loadAllView);
                loadAllView.setVisibility(View.GONE);
                start = 0;
                getDatas(RefreshType.PULL_DWON_TO_REFRESH, start);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
//                mScoreAdapter.reMoveFooterView(loadAllView);
                if (returnCode == 1) {
                    start++;
                    getDatas(RefreshType.PULL_UP_TO_REFRESH, start);
                } else if (returnCode == 2) {
                    loadAllView.setVisibility(View.VISIBLE);
                    mRecycleView.onRefreshComplete();
                }

//                if (haveAnother) {
//
//                } else {
//                    mRecycleView.onRefreshComplete();
//                    mScoreAdapter.addFooterView(loadAllView);
//                }
            }
        });
        loadAllView = new TextView(getActivity());
        loadAllView.setPadding(0, DensityUtil.dip2px(getActivity(), 10), 0, DensityUtil.dip2px(getActivity(), 10));
        loadAllView.setGravity(Gravity.CENTER);
        loadAllView.setTextSize(15);
        loadAllView.setText("已加载全部");
        mScoreAdapter = new ScoreListAdapter(getActivity(), mDatas, R.layout.item_order_list);
        loadAllView.setVisibility(View.GONE);
        mScoreAdapter.addFooterView(loadAllView);

        mScoreAdapter.setOnItemClickListener(WholeFragment.this);
        mRecycleView.getRefreshableView().setAdapter(mScoreAdapter);
    }

    int tid = 0;
    int returnCode = 0;

    private void getDatas(final RefreshType type, int start) {
        if (postion == 0) {
            tid = 0;
        } else if (postion == 1) {
            tid = 1;
        } else if (postion == 2) {
            tid = 2;
        } else if (postion == 3) {
            tid = 3;
        }
        mHttpUtil.getMyOrder(new ProgressSubscriber<ReturnMsg>(context) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        score_quesheng.setVisibility(View.VISIBLE);
                        if (mRecycleView != null) {
                            mRecycleView.onRefreshComplete();
                        }
                        break;
                    case 1://还有下一页
                        score_quesheng.setVisibility(View.GONE);
//                        haveAnother = true;
//                        Type type = new TypeToken<List<Order>>() {
//                        }.getType();
//                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
//                        mDatas = (ArrayList<Order>) fromJson;
//                        mScoreAdapter.notifyDataSetChanged();
                        returnCode = 1;
                        break;
                    case 2://没有下一页
                        score_quesheng.setVisibility(View.GONE);
//                        haveAnother = false;
                        returnCode = 2;
                        if (null != mRecycleView) {
                            mRecycleView.onRefreshComplete();
                        }
                        Type type = new TypeToken<List<Order>>() {
                        }.getType();
                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                        mDatas = (ArrayList<Order>) fromJson;
                        mScoreAdapter.setDatas(mDatas);
                        mRecycleView.onRefreshComplete();

                        break;
                }
            }
        }, "orderlist", mSession.getUserInfo().getId(), tid, start, 10);
    }

    @Override
    public void onItemClickListener(int posotion) {

    }

    class ScoreListAdapter extends SlideRecycleViewAdapter<Order> {


        /**
         * 构造器
         *
         * @param context
         * @param mDatas
         * @param itemLayoutId
         */
        protected ScoreListAdapter(Context context, ArrayList<Order> mDatas, int itemLayoutId) {
            super(context, mDatas, itemLayoutId);
        }

        @Override
        public void convert(SlideViewHolder holder, final Order item) {
            TextView order_create_time = holder.findViewById(R.id.order_create_time);
            TextView order_product_name = holder.findViewById(R.id.order_product_name);
            TextView order_item1 = holder.findViewById(R.id.order_item1);
            TextView order_item2 = holder.findViewById(R.id.order_item2);
            TextView order_item3 = holder.findViewById(R.id.order_item3);
            TextView order_price = holder.findViewById(R.id.order_price);
            TextView order_total_price = holder.findViewById(R.id.order_total_price);
            Button order_detail_btn = holder.findViewById(R.id.order_detail_btn);
            Button order_evaluate_btn = holder.findViewById(R.id.order_evaluate_btn);

            AppUtils.setText(order_item1, item.getDets().get(0).getProductattribute().getProject());
            AppUtils.setText(order_item2, item.getDets().get(0).getProductattribute().getMethod());
            AppUtils.setText(order_item3, item.getDets().get(0).getProductattribute().getStandard());
            AppUtils.setText(order_price, item.getDets().get(0).getPayprice() + "");

            AppUtils.setText(order_create_time, item.getCreatetime());
            AppUtils.setText(order_product_name, item.getName());
            AppUtils.setText(order_total_price, "￥" + item.getTotalprice());

            order_detail_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, OrderDetailActivity.class);
                    intent.putExtra("OrderID", item.getId());
                    startActivity(intent);
                }
            });
            order_evaluate_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, EvaluateActivity.class);
                    intent.putExtra(ConstantValues.EVALUATE_INTENT,item.getId());
                    startActivity(intent);
                }
            });
        }

    }

    enum RefreshType {
        PULL_DWON_TO_REFRESH,
        PULL_UP_TO_REFRESH
    }

    /**
     * 订单状态Enum
     * 1 待付款
     * 2 已付款
     * 3 4 5 6 7 等待检测结果
     * 8 检测完成
     */
//    enum PayState {
//
//    }

}
